package com.interview.controller;

import com.interview.common.Result;
import com.interview.common.ResultMessage;
import com.interview.common.StatusCode;
import com.interview.entity.*;
import com.interview.service.DataUpLoadService;
import com.interview.service.ScheduleService;
import com.interview.service.DrawLotsService;
import com.interview.service.SignedInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("drawLots")
public class DrawLotsController {
    @Autowired
    private DrawLotsService drawLotsService;
    @Autowired
    private SignedInfoService signedInfoService;
    @Autowired
    private ScheduleService scheduleService;
    @Autowired
    private DataUpLoadService dataUpLoadService;

    @PostMapping("generateDrawLotsNumber")
    public Result generateDrawLotsNumber() {
        List<SignedInfo> allSignedExamee = signedInfoService.getAllSignedExamee();
        for (SignedInfo examee : allSignedExamee) {
            String idNumber = examee.getIdNumber();
            ExameeDrawLots drawLots = drawLotsService.drawLotsOneByOne(idNumber);
            System.out.println("抽签结果为： "+drawLots.toString());
            drawLotsService.savaDrawLotsResult(drawLots);
            Schedule drawLotsSchedule = new Schedule(drawLots.getGroupId(), drawLots.getDrawLotsNumber());
            scheduleService.removeDrawLots(drawLotsSchedule);
        }
        return new Result(true, StatusCode.OK, ResultMessage.Draw_Success,null);
    }

    @PostMapping("getDrawLotNumberWithSignedInfo")
    public Result getDrawLotNumberWithSignedInfo(String idNumber) {
        List<SignedWithDrawLot> signedWithDrawLots=drawLotsService.getDrawLotNumberWithSignedInfo();
        return new Result(true, StatusCode.OK, ResultMessage.Draw_Success,signedWithDrawLots);
    }

    @PostMapping("getDrawLotsList")
    public Result getDrawLotsList() {
        List<ExameeDrawLots> drawLotsList=drawLotsService.getDrawLotsList();
        return new Result(true, StatusCode.OK, ResultMessage.Draw_Success,drawLotsList);
    }

    @PostMapping("upLoadDrawLotsAndAbsentInfo")
    public Result upLoadDrawLotsAndAbsentInfo() {
        //获取缺考信息
        List<SignedStatics> signedStaticsList = signedInfoService.getSignedStaticsList();
        ArrayList<String> strings = new ArrayList<>();
        for (SignedStatics signedStatics : signedStaticsList) {
            if (Integer.parseInt(signedStatics.getNotSigned())!=0){
                String groupId = signedStatics.getGroupId();
                String notSigned = signedStatics.getNotSigned();
                List<String> drawLotsAwaitList = scheduleService.getDrawLotsAwaitList(groupId);
                if (Integer.parseInt(notSigned)==drawLotsAwaitList.size()) {
                    strings.add(groupId);
                }
            }
        }
        if (strings.size()>0) {
            HashMap<String, String> hashMap = new HashMap<>();
            for (String groupId : strings) {
                List<String> drawLotsAwaitList = scheduleService.getDrawLotsAwaitList(groupId);
                String joined = String.join(",", drawLotsAwaitList);
                hashMap.put(groupId,joined);
            }
            boolean tag = dataUpLoadService.upLoadAbsent(hashMap);
            //获取抽签列表
            List<ExameeDrawLots> drawLotsList = drawLotsService.getDrawLotsList();
            //待完善
            boolean success=dataUpLoadService.upLoadDrawLots(drawLotsList);

            if (tag==true){
                return new Result(true, StatusCode.OK, ResultMessage.OPERATION_SUCCESS,null);
            }else {
                return new Result(false, StatusCode.ERROR, ResultMessage.OPERATION_FAILED,null);
            }
        }else {
            return new Result(false,StatusCode.ERROR,ResultMessage.OPERATION_FAILED,null);
        }

    }
}
